package net.stuffrepos.bricknetcenter;

import java.awt.SplashScreen;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JScrollPane;
import net.stuffrepos.bricknetcenter.database.DataManager;
import net.stuffrepos.bricknetcenter.database.DatabaseManager;
import net.stuffrepos.bricknetcenter.database.schema.Information;
import net.stuffrepos.bricknetcenter.database.schema.Information.Type;
import net.stuffrepos.bricknetcenter.gui.MainFrame;
import net.stuffrepos.bricknetcenter.gui.inventories.ObjectPicker;
import net.stuffrepos.bricknetcenter.plugins.bricklink.BrickLinkHttpProvider;
import net.stuffrepos.bricknetcenter.plugins.peeron.PeeronHttpProvider;
import net.stuffrepos.bricknetcenter.provider.HttpProviderManager;
import net.stuffrepos.userdesktopdatabase.Database;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Hello world!
 *
 */
public class App {

    private final static Log log = LogFactory.getLog(App.class);

    public static void main(String[] args) throws Exception {
        Options options = new Options();
        options.addOption("c", "config-id", true, null);
        options.addOption(null, "clear-database", false, null);
        options.addOption(null, "provider-bricklink", true, null);
        options.addOption(null, "provider-peeron", true, null);

        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("config-id")) {
            DatabaseManager.setDatabaseId(cmd.getOptionValue("config-id"));
        }

        DatabaseManager.setClearDatabase(cmd.hasOption("clear-database"));

        if (Boolean.parseBoolean(cmd.getOptionValue("provider-bricklink", "true"))) {
            HttpProviderManager.getInstance().addHttpProvider(new BrickLinkHttpProvider());
        }
        
        if (Boolean.parseBoolean(cmd.getOptionValue("provider-peeron", "true"))) {
            HttpProviderManager.getInstance().addHttpProvider(new PeeronHttpProvider());
        }

        runApp();
        //exportSchema();
        //runHttpDownloader();
        //testObjectPicker();
        //testBigDataList();
    }

    private static void runApp() throws Exception {
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }
        DatabaseManager.getInstance().requestDatabase(Database.LAST_VERSION);

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                splash.close();
                MainFrame.getInstance().setVisible(true);
                try {
                    HttpProviderManager.getInstance().start();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    private static void exportSchema() {
        DatabaseManager.getInstance().requestDatabase(Database.LAST_VERSION);
        DatabaseManager.getInstance().getHibernateUtil().exportSchema();
    }

    private static void runHttpDownloader() throws Exception {
        //DatabaseManager.getInstance().requestDatabase(3);
        DatabaseManager.getInstance().requestDatabase(Database.LAST_VERSION);
        HttpProviderManager.getInstance().addHttpProvider(new PeeronHttpProvider());
        HttpProviderManager.getInstance().addHttpProvider(new BrickLinkHttpProvider());
        HttpProviderManager.getInstance().start();

    }

    private static void testObjectPicker() {
        DatabaseManager.getInstance().requestDatabase(Database.LAST_VERSION);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ObjectPicker dialog = new ObjectPicker(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    public static void close() {
        log.info("Closing application.");
        DataManager.getInstance().stop();
        try {
            HttpProviderManager.getInstance().stop();
        } catch (Exception ex) {
            log.warn(ex.getMessage(), ex);
        }
    }

    private static void testBigDataList() {
        DatabaseManager.getInstance().requestDatabase(Database.LAST_VERSION);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    JDialog dialog = new JDialog();
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setTitle("Big List Test");
                    DefaultListModel listModel = new DefaultListModel();
                    for (Information information : DataManager.getInstance().getInformationManager().findAllInformations(Type.Set).fetchAll()) {
                        listModel.addElement(information);
                    }
                    JScrollPane scroll = new JScrollPane();
                    JList list = new JList();
                    list.setModel(listModel);
                    scroll.getViewport().setView(list);
                    dialog.add(scroll);
                    dialog.pack();
                    dialog.setVisible(true);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}
